﻿namespace PhoneFilter
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SQLite;
    using System.IO;

    public class SqliteManager
    {
        public static string ABLED = "可用";
        private static string ConnectString = "Data Source=data";
        private static string dbPath = "data";
        public static string DISABLED = "不可用";
        public static string FAIL = "失败";
        public static string IS4G = "4G卡";
        public static string NOT4G = "非4G卡";
        public static string NOTBEGIN = "尚未开始";
        private static string pass = null;
        public static string QITA = "未识别";
        public static string QUERYING = "正在查询";
        public static string SUCCESS = "成功";

        public static bool ChangePassword(string path, string newPassword, string oldPassword)
        {
            try
            {
                SQLiteConnection connection = new SQLiteConnection(ConnectString);
                connection.Open();
                connection.ChangePassword(newPassword);
                connection.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool CheckDb()
        {
            bool flag = false;
            try
            {
                if (File.Exists(dbPath))
                {
                    return true;
                }
                SQLiteConnection.CreateFile(dbPath);
                flag = ChangePassword(dbPath, pass, null);
                if (flag)
                {
                    flag = CreateTable();
                }
            }
            catch (Exception exception)
            {
                flag = false;
                throw exception;
            }
            return flag;
        }

        public static bool CreateTable()
        {
            bool flag = false;
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
                {
                    connection.Open();
                    string commandText = "create table phonelist([phone] varchar(50),[state] varchar(50));";
                    SQLiteCommand command = new SQLiteCommand(commandText, connection);
                    command.ExecuteNonQuery();
                    commandText = "create index phonelist_id on phonelist( phone )";
                    command.CommandText = commandText;
                    command.ExecuteNonQuery();
                    commandText = "create table phone086([phone] varchar(50),[imei] varchar(50), [regstate] varchar(50), [blstate] varchar(50), [regreason] varchar(500), [reason] varchar(500));";
                    command.CommandText = commandText;
                    command.ExecuteNonQuery();
                    commandText = "create index phone086_id on phone086( phone )";
                    command.CommandText = commandText;
                    command.ExecuteNonQuery();
                    commandText = "create table phonecommon([phone] varchar(50),[blstate] varchar(50), [reason] varchar(500), [cityname] varchar(500),[userinfo] varchar(5000));";
                    command = new SQLiteCommand(commandText, connection);
                    command.ExecuteNonQuery();
                    commandText = "create index phonecommon_id on phonecommon( phone )";
                    command.CommandText = commandText;
                    command.ExecuteNonQuery();
                }
                flag = true;
            }
            catch (Exception)
            {
            }
            return flag;
        }

        public static DataSet ExecuteDataSet(string sql)
        {
            DataSet dataSet = new DataSet();
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
                {
                    SQLiteCommand cmd = new SQLiteCommand(sql, connection);
                    connection.Open();
                    new SQLiteDataAdapter(cmd).Fill(dataSet);
                }
            }
            catch (Exception)
            {
            }
            return dataSet;
        }

        public static bool ExecuteNonQuery(string sql)
        {
            bool flag = false;
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
                {
                    SQLiteCommand command = new SQLiteCommand(sql, connection);
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                flag = true;
            }
            catch (Exception)
            {
            }
            return flag;
        }

        public static object ExecuteScalar(string sql)
        {
            object obj2 = null;
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
                {
                    SQLiteCommand command = new SQLiteCommand(sql, connection);
                    connection.Open();
                    obj2 = command.ExecuteScalar();
                }
            }
            catch (Exception)
            {
            }
            return obj2;
        }

        public static void ImportPhone(ArrayList list)
        {
            using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
            {
                connection.Open();
                SQLiteTransaction transaction = connection.BeginTransaction();
                try
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        string str2 = list[i].ToString();
                        new SQLiteCommand(string.Format("insert into phonelist(phone, state) values('{0}','{1}')", str2, NOTBEGIN), connection).ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                }
            }
        }

        public static void ImportPhone086(ArrayList list)
        {
            using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
            {
                connection.Open();
                SQLiteTransaction transaction = connection.BeginTransaction();
                try
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        string[] strArray = (string[]) list[i];
                        string str2 = strArray[0];
                        string str3 = strArray[1];
                        new SQLiteCommand(string.Format("insert into phone086(phone, imei, regstate, blstate) values('{0}','{1}','{2}','{3}')", new object[] { str2, str3, NOTBEGIN, NOTBEGIN }), connection).ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                }
            }
        }

        public static void ImportPhoneCommon(ArrayList list)
        {
            using (SQLiteConnection connection = new SQLiteConnection(ConnectString))
            {
                connection.Open();
                SQLiteTransaction transaction = connection.BeginTransaction();
                try
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        string str2 = list[i].ToString();
                        new SQLiteCommand(string.Format("insert into phonecommon(phone, blstate) values('{0}','{1}')", str2, NOTBEGIN), connection).ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                }
            }
        }
    }
}

